//
//  XCTabBarController.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/5.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

class XCTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //设置全局字体
//        UINavigationBar.appearance().backgroundColor = UIColor.orangeColor()
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        
        //创建一个tabBar类来自定义tabBar  因为tabBarController中的tabBar不能修改
        let myTabBar = XCTabBar()
        //测试 自定义的tabBar是否定义成功 若两次打印的地址不成功  则说明tabBar设置成功
//        print("系统的\(tabBar)")
        //通过KVC为tabBarCOntroller赋值
        setValue(myTabBar, forKey: "tabBar")
//        print("自定义的\(tabBar)")
        
        addChildViewControllers()

        //为打搅按钮添加点击事件

        myTabBar.addButton?.addTarget(self, action: "addButtonClick", forControlEvents: .TouchUpInside)
        
    }
    
       
    //定义加号按钮的点击事件
    func addButtonClick () {
        
        print("大加号的点击事件\(__FUNCTION__)")
        
        let compose = XCComposeController()
        let nav = UINavigationController(rootViewController: compose)
        
        presentViewController(nav, animated: true, completion: nil)
    }

    //添加controller
    func addChildViewControllers() {
        
        addchild(XCHomeController(), title: "主页", image: "tabbar_home", selectImage: "tabbar_home_selected")
        addchild(XCMessageController() ,title: "消息", image: "tabbar_message_center", selectImage: "tabbar_message_center_selected")
        addchild(XCDiscoverController(), title: "发现", image: "tabbar_discover", selectImage: "tabbar_discover_selected")
        addchild(XCProfileController(), title: "我", image: "tabbar_profile", selectImage: "tabbar_profile_selected")
    }
  
    //添加NavigationController
    func addchild(vc: UIViewController , title : String ,image: String , selectImage : String){
     
        let navi = UINavigationController(rootViewController: vc)

        addChildViewController(navi)

        //设置图片以及文字
        vc.tabBarItem.selectedImage = UIImage(named: selectImage)?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.image = UIImage(named: image)
        vc.tabBarItem.title = title
//        vc.tabBarController?.tabBar.tintColor = UIColor.orangeColor()
        
//        vc.tabBarItem.image?.renderingMode as UIImageRenderingModeAlwaysOriginal
        
    }
    
    
}
